#include "mainwindow.h"
#include "ui_mainwindow.h"


/******** Module Control Interface *************/
void MainWindow::Initialization()
{
    glabalInfo.subModStus.InitializationStus = 0;
    qDebug()<<"Initialization"<<endl;

    setEPC(0, 0);
    delay(200);
    setEPC(1, 0);
    delay(200);
    setEPC(2, 68);
    delay(5000); //wait for stabilization
    ReadEPC(EPC_MODE_PRESS); //
    //read 3 EPC

    setValve(1, 0);
    setValve(2, 0);
    setValve(3, 0);
    setValve(4, 0);
    setValve(5, 0);
    setValve(6, 0);
    setValve(7, 0);
    setValve(8, 0);

  //  setMFC(0);
    MainWindow_log("Initialization Finished...");
    qDebug()<<"Initialization end"<<endl;
    glabalInfo.subModStus.InitializationStus = 1; //indicate Initial finish
}

void MainWindow::PurgeDehydration()
{
    glabalInfo.subModStus.PurgeDehydrationStus = 0;
    setValve(1, 1);
    setHeater(0,3.0);
    setValve(5, 1);
    delay(1000); //delay X s

    setValve(5, 0);
    delay(1000);
    setValve(1, 0);
    MainWindow_log("PurgeDehydration Finished...");
    glabalInfo.subModStus.PurgeDehydrationStus = 1;
}

void MainWindow::PurgeConcentrationA()
{
#if 1
    glabalInfo.subModStus.PurgeConcentrationAStus = 0;
    setValve(6, 0);
    delay(2000);

    setValve(8, 0);
    delay(2000);

    setEPC(1, 68);
    delay(5000);

    setHeater(1,3.0);
    delay(1000);//delay X s
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("PurgeConcentrationA Finished...");
    glabalInfo.subModStus.PurgeConcentrationAStus = 1;
#endif
//    glabalInfo.subModStus.PurgeConcentrationAStus = 0;
//    qDebug()<<"A1"<<endl;
//    setValve(6, 0);
//    delay(2000);

//    qDebug()<<"A2"<<endl;
//    setValve(8, 0);
//    delay(2000);
//    glabalInfo.subModStus.PurgeConcentrationAStus = 1;
}

void MainWindow::PurgeConcentrationB() //暂时不用
{
#if 1
    glabalInfo.subModStus.PurgeConcentrationBStus = 0;
    setValve(6, 1); //ERV ON
    delay(2000);
    setValve(8, 0);
    delay(2000);
    setEPC(1, 68);
    delay(5000);
    //setHeater(2,3.0);
    delay(1000); //delay X second
    setEPC(1, 0);
    delay(5000);
    setValve(6, 0); //ERV OFF
    delay(2000);
    MainWindow_log("PurgeConcentrationB Finished...");
    glabalInfo.subModStus.PurgeConcentrationBStus = 1;
#endif
//    glabalInfo.subModStus.PurgeConcentrationBStus = 0;
//    qDebug()<<"B1"<<endl;
//    setValve(6, 1);
//    delay(2000);

//    qDebug()<<"B2"<<endl;
//    setValve(8, 1);
//    delay(2000);
//    glabalInfo.subModStus.PurgeConcentrationBStus = 1;
}

void MainWindow::PurgeFocusing() //暂时不用
{
    glabalInfo.subModStus.PurgeFocusingStus = 0;
    setValve(6, 0); //ERV ON
    delay(2000);
    setValve(8, 0);
    delay(2000);
    setEPC(1, 68);
    delay(5000);
    //setHeater(3,3.0);
    delay(3000); //delay X second
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("PurgeFocusing Finished...");
    glabalInfo.subModStus.PurgeFocusingStus = 1;
}

void MainWindow::PreSampleA() //暂时不用
{
    glabalInfo.subModStus.PreSampleAStus = 0;
    setValve(3, 1);
    setValve(6, 0);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(3000);  //Delay Y seconds
    setValve(7, 0);
    delay(1000);
    MainWindow_log("PreSampleA Finished...");
    glabalInfo.subModStus.PreSampleAStus = 1;
}
void MainWindow::PreSampleB()
{
    glabalInfo.subModStus.PreSampleBStus = 0;
    setValve(3, 0);
    setValve(6, 1);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(3000);  //Delay Y seconds
    setValve(7, 0);
    delay(1000);
    MainWindow_log("PreSampleB Finished...");
    glabalInfo.subModStus.PreSampleBStus = 1;
}
void MainWindow::SampleA()
{
    glabalInfo.subModStus.SampleAStus = 0;
    setValve(3, 0);
    setValve(6, 0);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(3000);  //Delay Y minutes
    setValve(7, 0);
    delay(1000);
    MainWindow_log("SampleA Finished...");
    glabalInfo.subModStus.SampleAStus = 1;
}
void MainWindow::SampleB()
{
    glabalInfo.subModStus.SampleBStus = 0;
    setValve(3, 1);
    setValve(6, 1);
    delay(2000);
    setMFC(500);  //Set MFC to X sccm
    setValve(7, 1);
    delay(60000);  //Delay Y minutes
    setValve(7, 0);
    delay(1000);
    MainWindow_log("SampleB Finished...");
    glabalInfo.subModStus.SampleBStus = 1;
}
void MainWindow::PrePurgeA()
{
    glabalInfo.subModStus.PrePurgeAStus = 0;
    setValve(6, 1);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y seconds
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("PrePurgeA Finished...");
    glabalInfo.subModStus.PrePurgeAStus = 1;
}
void MainWindow::PrePurgeB()
{
    glabalInfo.subModStus.PrePurgeBStus = 0;
    setValve(6, 0);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y second
    MainWindow_log("PrePurgeB Finished...");
    glabalInfo.subModStus.PrePurgeBStus = 1;
}
void MainWindow::ConTrapFireA()
{
    glabalInfo.subModStus.ConTrapFireAStus = 0;
    setValve(6, 1);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y second
    setHeater(1, 3.5);  //Set Heater 1 to Timer Y
    delay(4000);
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("ConTrapFireA Finished...");
    glabalInfo.subModStus.ConTrapFireAStus = 1;
}
void MainWindow::ConTrapFireB()
{
    glabalInfo.subModStus.ConTrapFireBStus = 0;
    setValve(6, 0);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y second
    //setHeater(2, 3.5);  //Set Heater 1 to Timer Y
    delay(4000);
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("ConTrapFireB Finished...");
    glabalInfo.subModStus.ConTrapFireBStus = 1;
}
void MainWindow::FocusTrapFire()
{
    glabalInfo.subModStus.FocusTrapFireStus = 0;
    setValve(8, 1);
    setEPC(2, 68); //Set EPC 2 to X psi
    delay(5000);
    //setHeater(3, 3.5);
    delay(5000);
    setValve(8, 0);
    delay(2000);
    MainWindow_log("FocusTrapFire Finished...");
    glabalInfo.subModStus.FocusTrapFireStus = 1;
}
void MainWindow::StandardA()
{
    glabalInfo.subModStus.StandardAStus = 0;

    qDebug()<<"StandardA"<<endl;

    setValve(2, 1);
    setValve(4, 1);
    delay(2000);
    setValve(3, 0);
    setValve(6, 0);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(2000);  //Sample Y minutes
    setValve(7, 0);
    delay(2000);
    setValve(4, 0);
    setValve(2, 0);
    MainWindow_log("StandardA Finished...");
    glabalInfo.subModStus.StandardAStus = 1;
}
void MainWindow::StandardB()
{
    glabalInfo.subModStus.StandardBStus = 0;
    setValve(2, 1);
    setValve(4, 1);
    delay(2000);
    setValve(3, 1);
    setValve(6, 1);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(2000);  //Sample Y minutes
    setValve(7, 0);
    delay(2000);
    setValve(4, 0);
    setValve(2, 0);
    MainWindow_log("StandardB Finished...");
    glabalInfo.subModStus.StandardBStus = 1;
}


void MainWindow::readGC_stus()
{
    glabalInfo.subModStus.GC_readStus = 0;
    quint16 addr_GC_read_status = 14;
    //quint16 GC_read_status = 2; //2     indicate unknown status
    mutex_epcmfc->lock();
    GC_read_status = sendReadHoldingReg(SerDev_EPC_MFC, EPC_MB_SLAVE_ID, addr_GC_read_status);
    mutex_epcmfc->unlock();
    qDebug()<<"GC_read_status: "<<GC_read_status<<endl;
    switch(GC_read_status)
    {
     case 0:
        qDebug()<<"GC ready"<<endl;
        MainWindow_log("readGCInfo: GC ready");
        break;
     case 1:
       qDebug()<<"GC not-ready"<<endl;
       MainWindow_log("readGCInfo: GC not-ready");
        break;
    default:
        qDebug()<<"GC unkonwn"<<endl;
        MainWindow_log("readGCInfo: GC unkonwn");
        break;
    }


    glabalInfo.subModStus.GC_readStus = 1;
}




/*---------- Asynchronize ----------*/
void myThread::run()
{
    //3:mid   6:epc
#define delay_ms      QThread::msleep
#define delay_second  QThread::sleep
#define SET_VALVE     QThread::msleep(200),emit sig_setValve
//#define SET_VALVE     emit sig_setValve
#define SET_EPC       QThread::msleep(200),emit sig_setEPC
#define SET_TEC       QThread::msleep(200),emit sig_setTEC
#define SET_MFC       QThread::msleep(200),emit sig_setMFC
#define SET_HEATER    QThread::msleep(200),emit sig_setHeater
#define SET_HEAT_TUBE QThread::msleep(200),emit sig_setHeatingTube



    /* sub-flow interface */
#define Initialization_      emit sig_Initialization
#define READ_EPC             emit sig_ReadEPC
#define PurgeDehydration_    emit sig_PurgeDehydration
#define PurgeConcentrationA_ emit sig_PurgeConcentrationA
#define PurgeConcentrationB_ emit sig_PurgeConcentrationB
#define PurgeFocusing_       emit sig_PurgeFocusing
#define PreSampleA_          emit sig_PreSampleA
#define PreSampleB_          emit sig_PreSampleB
#define SampleA_             emit sig_SampleA
#define SampleB_             emit sig_SampleB
#define PrePurgeA_           emit sig_PrePurgeA
#define PrePurgeB_           emit sig_PrePurgeB
#define ConTrapFireA_        emit sig_ConTrapFireA
#define ConTrapFireB_        emit sig_ConTrapFireB
#define FocusTrapFire_       emit sig_FocusTrapFire
#define StandardA_           emit sig_StandardA
#define StandardB_           emit sig_StandardB

#define SetGC                emit sig_setGC   //打开GC: SetGC(true)   关闭GC: SetGC(false)
#define ReadGC_Stus          emit sig_readGC_stus
/*
    The Interface of User for Invoke:
Note：
    delay milisecond： delay_ms(xx);
    delay second：     delay_second(xx);

set EPC                    void SET_EPC(uint8_t channel_num, float setValue);   // channel_num: (0,1,2)   setValue: xxx (kPa)
set Valve ON&Off           void SET_VALVE(uint8_t channel_num, uint16_t valve_status); //channel_num: (1,2,3,4,5,6,7,8,)   valve_status: 0 or 1
设置TEC制冷                  void SET_TEC(uint8_t channel_num, float temperature); //channel_num: (0,1)   temperature(°C): xxx °C
设置MFC流量值        void SET_MFC(uint16_t numOf_sccm);
设置热脱附加热时间    void SET_HEATER(uint8_t channel_num, float num_of_seconds);  //channel_num: (0,1)   num_of_seconds(s): xxx s
设置伴热管加热温度    void SET_HEAT_TUBE(uint8_t channel_num, float target_temperature);  //channel_num:(0,1)


*/
//    Initialization_();
//    while(!glabalInfo.subModStus.InitializationStus);
    qDebug()<<"---------Initialization----------"<<endl;


    //int loop_times = 10;

    while(is_run)
    {
#if 1
        Initialization_();
        Initialization_wait(); //等待子流程执行完成


        StandardA_();
        StandardA_wait();

        ConTrapFireA_();
        ConTrapFireA_wait();
#endif
        SetGC(true);

        ReadGC_Stus();
        ReadGC_Stus_wait();
        delay_second(1);
        SetGC(false);

        qDebug()<< GC_read_status <<endl; //打印读取到的GC状态


        qDebug()<<"<><><>"<<endl;
        is_run = false;
    }
}

/*
Log:

2021-03-24 Suzhou
1. midway board new add 2 channel heater (Sting and UMB)  finish
2. test all sub-flow control.
3. Debug EPC stability
4. FW new add when set hotline target temp<10, PID stop heater

Caution:
    ask FangJun : Eject channel Tranfer Hotline is TC, not pt100
        need change probeFilter sensor to Eject channel

*/


